﻿#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"

//封装一个设置光标位置的函数
void SetPos(short x, short y)
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

//欢迎界面和功能介绍页面
void WelcomeToGame()
{
	SetPos(42, 14);
	printf("欢迎来到贪吃蛇小游戏.......\n");
	SetPos(46, 18);
	system("pause");
	system("cls");
	SetPos(30, 12);
	wprintf(L"使用↑ . ↓ . ← . →来控制蛇的方向,F3为加速,F4为减速\n");
	SetPos(30, 13);
	wprintf(L"加速将能得到更高的分数.\n");
	SetPos(35, 18);
	system("pause");
	system("cls");
}

//创建地图
void CreateMap()
{
	//上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 27);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (int j = 1; j <= 26; j++)
	{
		SetPos(0, j);
		wprintf(L"%lc", WALL);
	}
	//右
	for (int j = 1; j <= 26; j++)
	{
		SetPos(56, j);
		wprintf(L"%lc", WALL);
	}


	system("pause");
}

//初始化蛇
void InitSnake(Snake* ps)
{
	SnakeNode* pcur = NULL;
	int i = 0;
	//创建蛇身节点，并初始化坐标
	//头插法
	for (i = 0; i < 5; i++)
	{
		//创建蛇身节点
		pcur = (SnakeNode*)malloc(sizeof(SnakeNode));
		if (pcur == NULL)
		{
			perror("InitSnake::malloc");
			return 1;
		}
		//设置坐标
		pcur->next = NULL;
		pcur->x = POS_X + i * 2;
		pcur->y = POS_Y;

		//头插法
		if (ps->psnake == NULL)
		{
			ps->psnake = pcur;
		}
		else
		{
			pcur->next = ps->psnake;
			ps->psnake = pcur;
		}

		//打印蛇的身体
		pcur = ps->psnake;
		while (pcur)
		{
			SetPos(pcur->x, pcur->y);
			wprintf(L"%lc", BODY);
			pcur = pcur->next;
		}

		//初始化贪吃蛇数据
		ps->sleeptime = 200;
		ps->socre = 0;
		ps->status = OK;
		ps->dir = RIGHT;
		ps->foodscore = 10;
	}
}

//创造第⼀个⻝物
void CreateFood(Snake* ps)
{
	int x = 0;
	int y = 0;

	again:
	//产生的x坐标应该是2的背书，这样才能和蛇头坐标对齐。
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);

	SnakeNode* pcur = ps->psnake;//获取指向蛇头的指针
	//食物不能和蛇身冲突
	while (pcur)
	{
		if (pcur->x == x && pcur->y == y)
		{
			goto again;
		}
		pcur = pcur->next;
	}
	//创建食物
	SnakeNode* pFood = (SnakeNode*)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CrateFood::malloc");
		return 1;
	}
	else
	{

		pFood->x = x;
		pFood->y = y;
		SetPos(pFood->x, pFood->y);
		wprintf(L"%lc", FOOD);
		//维护到结构体中
		ps->pfood = pFood;
	}
}

//创建游戏的环境和菜单
void GameStart(Snake* ps)
{
	//设置游戏页面的行和列及窗口标题
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	//获取句柄
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//隐藏光标操作
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态
	//欢迎界面和功能介绍页面
	WelcomeToGame();
	//打印地图
	CreateMap();
	//初始化蛇
	InitSnake(ps);
	//创造第⼀个⻝物
	CreateFood(ps);
}

//打印右侧帮助信息
void PrintHelpInfo()
{
	//打印提⽰信息
	SetPos(64, 15);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(64, 16);
	printf("用↑ . ↓ . ← . →分别控制蛇的移动.");
	SetPos(64, 17);
	printf("F3 为加速，F4 为减速\n");
	SetPos(64, 18);
	printf("ESC ：退出游戏.space：暂停游戏.");
}
//吃食物
void EatFood(SnakeNode* pnext, Snake* ps)
{
	//头插法
	pnext->next = ps->psnake;
	ps->psnake = pnext;

	//打印蛇
	SnakeNode* pcur = ps->psnake;
	while (pcur)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%c", BODY);
		pcur = pcur->next;
	}
	ps->socre += ps->foodscore;

	//释放食物节点
	free(ps->pfood);
	//创建新的食物节点
	CreateFood(ps);
}
//撞墙
int KillByWall(Snake* ps)
{
	if ((ps->psnake->x == 0)
		|| (ps->psnake->x == 56)
		|| (ps->psnake->y == 0)
		|| (ps->psnake->y == 26))
	{
		ps->status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}

//撞自己
int KillBySelf(Snake* ps)
{
	SnakeNode* pcur = ps->psnake->next;
	while (pcur)
	{
		if ((ps->psnake->x == pcur->x)
			&& (ps->psnake->y == pcur->y))
		{
			ps->status = KILL_BY_SELF;
			return 1;
		}
		pcur = pcur->next;
	}
	return 0;
}
//下一个是食物
int NextIsFood(SnakeNode* pnext, Snake* ps)
{
	return (pnext->x == ps->pfood->x) && (pnext->y == ps->pfood->y);
}

//下一节点没有食物，继续向前走
void NoFood(SnakeNode* pnext, Snake* ps)
{
	//头插法
	pnext->next = ps->psnake;
	ps->psnake = pnext;

	//打印蛇
	SnakeNode* pcur = ps->psnake;
	while (pcur->next->next != NULL)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%c", BODY);
		pcur = pcur->next;
	}
	//最后一个位置打印空格，然后释放节点
	SetPos(pcur->next->x, pcur->next->y);
	printf("  ");
	free(pcur->next);
	pcur->next = NULL;
}

//蛇身移动
void SnakeMove(Snake* ps)
{
	//创建下一个节点
	SnakeNode* pNext = (SnakeNode*)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove::malloc");
		return 1;
	}
	//确定下一个节点的坐标，其坐标根据蛇头的坐标和方向确定
	switch (ps->dir)
	{
	case UP:
		pNext->x = ps->psnake->x;
		pNext->y = ps->psnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->psnake->x;
		pNext->y = ps->psnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->psnake->x-2;
		pNext->y = ps->psnake->y;
		break;
	case RIGHT:
		pNext->x = ps->psnake->x + 2;
		pNext->y = ps->psnake->y;
		break;
	}
	//如果下一个位置是食物
	if (NextIsFood(pNext, ps))
	{
		EatFood(pNext, ps);
	}
	else//如果没有食物
	{
		NoFood(pNext, ps);
	}
	//撞墙
	KillByWall(ps);
	//撞到了自己
	KillBySelf(ps);
}



//游戏运行
void GameRun(Snake* ps)
{
	//打印右侧帮助信息
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分: %d", ps->socre);
		printf("每个食物的得分：%d分\n", ps->foodscore);
		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			system("pause");
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = END_NORMAL;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->sleeptime >= 80)
			{
				ps->sleeptime -= 30;
				ps->foodscore += 2;//⼀个⻝物分数最⾼是20分
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->sleeptime < 320)
			{
				ps->sleeptime += 30;
				ps->foodscore -= 2;//⼀个⻝物分数最低是2分
			}
		}
		//蛇每次⼀定之间要休眠的时间，时间短，蛇移动速度就快
		Sleep(ps->sleeptime);
		SnakeMove(ps);
	} while(ps->status == OK);
}


//游戏结束
void GameEnd(Snake* ps)
{
	SnakeNode* pcur = ps->psnake;
	SetPos(24,12);
	switch (ps->status)
	{
	case END_NORMAL:
		printf("您主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("您撞上了自己，游戏结束！\n");
		break;
	case KILL_BY_WALL:
		printf("您撞墙了，游戏结束！\n");
		break;
	}
	//释放蛇身的节点
	while (pcur)
	{
		SnakeNode* del = pcur;
		pcur = pcur->next;
		free(del);
		del = NULL;
	}
}